package com.hongkzh.www.look.view.activity;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.hongkzh.www.R;
import com.hongkzh.www.look.model.bean.AttenedBean;
import com.hongkzh.www.look.model.bean.MyAttendBean;
import com.hongkzh.www.other.utils.SharedPreferencesUtil;
import com.hongkzh.www.other.utils.UIUtils;
import com.hongkzh.www.look.enterprise.presenter.MyAttendPresenter;
import com.hongkzh.www.view.activity.BaseActivity;
import com.hongkzh.www.look.view.adapter.AttSmlvAdapter;
import com.hongkzh.www.view.customview.MyFooter;
import com.hongkzh.www.look.view.iview.MyAttendIView;
import com.liaoinstan.springview.widget.SpringView;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.liaoinstan.springview.utils.DensityUtil.dp2px;

/**
 * 关注列表
 */
public class AttentionActivity extends BaseActivity<MyAttendPresenter> implements MyAttendIView, SpringView.OnFreshListener, AttSmlvAdapter.onItemClickListener, View.OnClickListener {

    @BindView(R.id.att_smlv)
    SwipeMenuListView attSmlv;
    @BindView(R.id.springview_attened)
    SpringView springViewMyAttened;

    private int pageNum = 1;

    private AttSmlvAdapter attSmlvAdapter;

    private boolean lastPage = false;

    private MyFooter myFooter;

    private String mLoginId;
    private SharedPreferencesUtil spfu;


    @Override
    protected void init() {
        spfu = new SharedPreferencesUtil(UIUtils.getContext());
        mLoginId = spfu.getUserInfo().getLoginUid();

        setPresenter(new MyAttendPresenter());

        mTiManager.setTitle("我的关注");
        mTiManager.setLeftIcon(R.mipmap.arrow_left);

        myFooter = new MyFooter(this);
        springViewMyAttened.setFooter(myFooter);

        attSmlvAdapter = new AttSmlvAdapter();
        attSmlv.setAdapter(attSmlvAdapter);
        getPresenter().GetMyAttendList(mLoginId, pageNum + "");

        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                // create "open" item
                SwipeMenuItem openItem = new SwipeMenuItem(AttentionActivity.this);
                // set item background
                openItem.setBackground(new ColorDrawable(Color.parseColor("#FF5252")));
                // set item width
                openItem.setWidth(dp2px(90));
                // set item title
                openItem.setTitle("删除");
                // set item title fontsize
                openItem.setTitleSize(12);
                // set item title font color
                openItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(openItem);
            }
        };
// set creator
        attSmlv.setMenuCreator(creator);
        attSmlv.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        //TODO: 取消关注的删除
                        getPresenter().GetFollowDelete(mLoginId, attSmlvAdapter.getAttendListByPosition(position).getFollowUserId());
                        attSmlvAdapter.remove(position);
                        attSmlvAdapter.notifyDataSetChanged();
                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });
        // Left
        attSmlv.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);

    }

    @Override
    protected void setListener() {
        mTiManager.getTitleLeft().setOnClickListener(this);
        springViewMyAttened.setListener(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_attention;
    }


    @Override
    public void onShowMyAttendList(MyAttendBean attendBean) {
        //显示我的关注列表
        if (pageNum == 1) {
            attSmlvAdapter.clearAttendList();
        }

        attSmlvAdapter.add(attendBean);
        attSmlvAdapter.notifyDataSetChanged();
        springViewMyAttened.onFinishFreshAndLoad();
    }

    @Override
    public void deleteMyAttendList(AttenedBean attendBean) {
        //删除对应条目，取消关注

    }

    @Override
    public void onLastPage(boolean lastPage) {
        this.lastPage = lastPage;
        myFooter.setLastPage(lastPage);

    }


    @Override
    public void onItemClick() {
        //点击子条目的监听
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Btn_titleLeft:
                finish();
                break;
        }
    }

    @Override
    public void onError(Exception e) {

    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadmore() {
        if (lastPage) {
            springViewMyAttened.onFinishFreshAndLoad();
        } else {
            pageNum++;
            getPresenter().GetMyAttendList(mLoginId, pageNum + "");
        }

    }
}
